import router from "@src/router";
import base from "@src/store/base";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { getCard } from '@src/api/member'
import { receiveBizCouponNewPre } from '@src/api/coupon'
import { matchCardCode } from '@src/common/coupon'
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['businessName', 'businessLogo', 'themeColor', 'themeTitleColor']
  }],
  data: {
    //置灰兑换按钮
    flag: true,
    code: null,
    //卡密
    secret: null,
    verifyLoading: false,
    //1：券 2.卡
    type: 1,
  },
  methods: {
    onLoad(options) {
      console.log('options', options);
      console.log(" (/^81(2|3)\d{16}$/g.test(res.result))", (/^81(28|30)\d{16}$/g.test(81908014676412276310)))
      if (options.code) {
        this.setData({ code: options.code, })
        if (options.type) { this.setData({ type: options.type }) }
        else {
          if (/^8128\d{16}$/g.test(this.data.code)) { this.setData({ type: 1 }); flag = true };
          if (/^8130\d{16}$/g.test(this.data.code)) { this.setData({ type: 2 }); flag = true };
        }
      }
    },
    onLogin() {
      if (this.data.code) {
        if (this.data.code.length > 5) { this.setData({ flag: false }) };
        if (this.data.type == 1) { this.getCouponFunc() } else { this.getCardFunc() }
      }
    },
    //领取卡
    getCardFunc() {
      this.setData({ verifyLoading: true })
      let self = this;
      let temp = {
        cardNo: this.data.code,
        secret: this.data.secret
      }
      getCard(temp).then(res => {
        if (res.code == 0) {
          this.setData({ verifyLoading: false })
          //不需要卡密
          if (res.data.finish) {
            wx.showModal({
              title: '兑换成功',
              content: '成功领取卡',
              confirmText: '查看详情',
              cancelText: '返回',
              success(res) {
                if (res.confirm) {
                  //去详情页
                  console.log('用户点击确定')
                  router.push({ name: 'member/couponDetailNew', params: { type: 2, model: 1, code: self.data.code } })
                } else if (res.cancel) {
                  //返回
                  console.log('用户点击取消')
                }
              }
            })
            //需要卡密
          } else {
            this.dialogShowFunc()
          }
        }
      }).catch(error => {
        this.setData({ verifyLoading: false })
      })
    },
    //领取券
    getCouponFunc() {
      let code = this.data.code;
      let self = this;
      receiveBizCouponNewPre(code).then(res => {
        if (res.code == 0) {
          let codes = res.data.codes;
          if (codes) {
            this.setData({ code: codes[0] })
          }
          wx.showModal({
            title: '兑换成功',
            content: '成功领取优惠券',
            confirmText: '查看详情',
            cancelText: '返回',
            success(res) {
              if (res.confirm) {
                //去详情页
                console.log('用户点击确定')
                router.push({ name: 'member/couponDetailNew', params: { type: 1, model: 1, couponCode: self.data.code } })
              } else if (res.cancel) {
                //返回
                console.log('用户点击取消')
              }
            }
          })

        }
      })
    },
    //点击扫描二维码
    scanHandleWx() {
      // console.log("点击扫描二维码");
      // router.push({ name: 'transfer' })
      wx.scanCode({
        success: res => {
          console.log('res', res);
          //todo 可能不是path 先这样写 后面再确定
          if (res.result) {
            //券code格式
            console.log("res.result.length", res.result.length)
            if ((/^81(28|30)\d{16}$/g.test(res.result))) {
              let result = res.result;
              if (/^8128\d{16}$/g.test(result)) { this.setData({ code: result, flag: false }) };
              if (/^8130\d{16}$/g.test(result)) { this.setData({ code: result, flag: false }) };
            } else {//普通链接
              let path = decodeURIComponent(res.result);
              console.log('path', path)
              let flag = matchCardCode(path);
              console.log('flag', flag)
              if (flag) {
                console.log('flag', flag);
                this.setData({ code: flag.id, flag: false })
              } else {
                wx.showToast({
                  title: '格式不正确',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          } else {
            wx.showToast({
              title: '格式不正确',
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    },
    //打开输入密码弹框
    dialogShowFunc() {
      this.selectComponent("#passwordCoupon").dialogShow({
        code: this.data.code,
        type: this.data.type
        // passRemark: this.data.couponDetail.remark,
        // couponInstructionArr: this.data.couponInstructionArr
      });
    },
    //点击兑换按钮
    exchangeFunc() {
      if (this.data.flag) {
        return;
      }
      let code = this.data.code;
      let str = /^8128\d{16}$/g;
      let cardStr = /^8130\d{16}$/g;
      console.log("code", code)
      //券的匹配规则
      let flag = false;
      if (str.test(code)) {
        console.log('券')
        this.getCouponFunc();
        this.setData({ type: 1 })
        flag = true
      }        //卡的匹配规则
      if (cardStr.test(code)) {
        console.log("卡")
        this.getCardFunc()
        this.setData({ type: 2 })
        flag = true
      }
      if (!flag) {
        wx.showToast({
          title: '您输入的格式不正确',
          icon: 'none',
          duration: 2000
        })
      }
    },
    bindinputFunc(val) {
      let len = val.detail.cursor;
      if (len > 5) { this.setData({ flag: false, code: val.detail.value }) } else { this.setData({ flag: true }) }
      console.log('val', val)
    }
  }
})
